/**
 * @created by kongqf on 2019-12-17 13:54:46
 * @updated by
 * @description wfdemo
 */
/* eslint no-undef:0 */
/* eslint new-cap:0 */

define([
  './libs/mxgraph/libs/init.js',
  './libs/mxgraph/mxClient.js',
  './pages/workflow/studio/nwflist/jWorkflow/jobFlowGraph.css',
  './libs/mxgraph/libs/sanitizer.min.js',
  './libs/mxgraph/libs/editorUi.js',
  './libs/mxgraph/libs/editor.js',
  './libs/mxgraph/libs/sidebar.js',
  './libs/mxgraph/libs/graph.js',
  './libs/mxgraph/libs/format.js',
  './libs/mxgraph/libs/shapes.js',
  './libs/mxgraph/libs/actions.js',
  './libs/mxgraph/libs/menus.js',
  './libs/mxgraph/libs/toolbar.js',
  './libs/mxgraph/libs/dialogs.js',
  './custom/widgets/js/YuWorkflow.js'
], function (require, exports) {
  /**
   * 页面加载完成时触发
   * @param hashCode 路由ID
   * @param data 传递数据对象
   * @param cite 页面站点信息
   */
  exports.ready = function (hashCode, data, cite) {
    // var size = yufp.frame.size();
    yufp.custom.vue({
      el: cite.el,
      data: function () {
        return {
          height: 390,
          value2: 100,
          viewScale: '100%',
          graphData: null,
          formItems: {},
          showSidebar: true,
          showFormatbar: true,
          workflowData: data,
          formatType: '',
          tmpCell: null,
          locked: true, // 是否预览模式，会自动屏蔽界面上输入操作
          returnBackFuncId: '',
          returnBackRootId: '',
          nodeInfo: [],
          comments: []
        };
      },
      mounted: function () {
        this.returnBackFuncId = data.returnBackFuncId;
        this.returnBackRootId = data.returnBackRootId;
        if (data.nodeInfo != null) {
          this.nodeInfo = data.nodeInfo;
        }
        this.initComment(data);
      },
      methods: {
        // 初始化左侧示例图标
        initGraph: function () {
          if (this.locked) {
            this.hideSidebarFn();
            this.hideFormatbarFn();
          }
          this.initData();
        },
        initComment: function (data) {
          var _this = this;
          var param = { mainInstanceId: '' };
          if (data.instanceId === data.mainInstanceId) {
            param.mainInstanceId = data.mainInstanceId;
          } else {
            param.mainInstanceId = data.instanceId;
          }
          yufp.service.request({
            url: backend.workflowService + '/api/core/getAllComments',
            data: param,
            callback: function (code, msg, response) {
              if (response.data != null) {
                var Comments = response.data;
                for (var k in Comments) {
                  var comment = Comments[k];
                  var nids = comment.nodeId.split('_');
                  var commentSign = yufp.lookup.convertKey('OP_TYPE', comment.commentSign);
                  var str = '流程实例号:' + comment.instanceId +
                  '&nbsp<br/>流程编号:' + nids[0] +
                  '&nbsp<br/>节点编号:' + nids[1] +
    		          '&nbsp<br/>节点名称:' + comment.nodeName +
                  '&nbsp<br/>审批人员:' + comment.userName +
                  '&nbsp<br/>审批结果:' + commentSign +
                  '&nbsp<br/>审批时间:' + comment.startTime +
                  '&nbsp<br/>审批意见:' + comment.userComment + '&nbsp';
                  comment = {
                    comment: str,
                    nid: nids[1] + ''
                  };
                  _this.comments.push(comment);
                }
              }
            }
          });
        },
        // 初始化绑定数据
        initData: function () {
          var _this = this;// 获取流程图
          yufp.service.request({
            url: backend.workflowService + '/api/nwfflow/flow',
            data: { flowId: data.flowId },
            callback: function (code, msg, response) {
              // console.log('获取流程图数据', response.data.flowContent);
              _this.graphData = response.data.flowContent;
              _this.graphData && (_this.workflowData = _this.$refs.refWorkflow.loadXML(_this.graphData));
              _this.$nextTick(function () {
                var cells = _this.$refs.refWorkflow.graph.getModel().cells;
                for (var c in cells) {
                  if (_this.nodeInfo == null) {
                    break;
                  }
                  for (var i = 0; i < _this.nodeInfo.length; i++) {
                    if (cells[c].vertex && cells[c].data.nid == _this.nodeInfo[i].nodeId.split('_')[1]) {
                      _this.$refs.refWorkflow.updateCellStyle(cells[c], 'shape', 'label');
                      _this.$refs.refWorkflow.updateCellStyle(cells[c], 'strokeWidth', 2);
                      _this.$refs.refWorkflow.updateCellStyle(cells[c], 'strokeColor', '#00FF7F');
                      _this.$refs.refWorkflow.updateCellStyle(cells[c], 'imageAlign', 'center');
                      _this.$refs.refWorkflow.updateCellStyle(cells[c], 'imageVerticalAlign', 'center');
                      // #TODO 修改此处图片尺寸，在高亮时，39会覆盖背后的动画
                      _this.$refs.refWorkflow.updateCellStyle(cells[c], 'imageWidth', 39);
                      _this.$refs.refWorkflow.updateCellStyle(cells[c], 'imageHeight', 39);
                      // 设置节点尺寸
                      _this.$refs.refWorkflow.setCellGeometry(cells[c], 40);
                      _this.$refs.refWorkflow.graph.scrollCellToVisible(cells[c], true);
                      var state = _this.$refs.refWorkflow.graph.view.getState(cells[c]);
                      state.shape.node.getElementsByTagName('rect')[0].setAttribute('class', 'flow');
                    }
                  }
                }
              });
            }
          });
        },
        // 节点单击事件
        cellclickFn: function (cell) {
          // console.log('节点点击事件', cell);
        },
        // 隐藏左侧面板
        hideSidebarFn: function () {
          this.$refs.refWorkflow.hideSidebar(this.showSidebar = !this.showSidebar);
        },
        // 隐藏右侧面板
        hideFormatbarFn: function () {
          this.$refs.refWorkflow.hideFormatbar(this.showFormatbar = !this.showFormatbar);
        }
      }
    });
  };

  /**
   * 页面传递消息处理
   * @param type 消息类型
   * @param message 消息内容
   */
  exports.onmessage = function (type, message, cite, vm) {
    if (type === 'active-tab') {
      vm.$refs.refWorkflow.hiddenOutlineWindow(false);
    }
    if (type === 'active-tab-self') {
      vm.$refs.refWorkflow.hiddenOutlineWindow(true);
    }
  };

  /**
   * 页面销毁时触发destroy方法
   * @param id 路由ID
   * @param cite 页面站点信息
   */
  exports.destroy = function (id, cite) {
  };
});